package vn.edu.saigontech.ncms.web.offering;

import java.util.Collection;
import java.util.Date;

import vn.edu.saigontech.ncms.service.OfferingService;
import vn.edu.saigontech.ncms.util.DateUtil;
import vn.edu.saigontech.ncms.util.Checker;
import vn.edu.saigontech.ncms.web.AbstractValidator;

@SuppressWarnings("serial")
public class ValidateAttendanceAction extends AbstractValidator {
	
	private Long offeringId;
	private Long[] memberIds;
	private String fromDate;
	private String toDate;
	
	public String check() {
		if(Checker.isNull(this.memberIds)) {
			this.sendKeyError("validator.required", "student");
		}
		if(Checker.isEmpty(this.fromDate)) {
			this.sendKeyError("validator.required", "fromDate");
		}
		if(!Checker.isDate(this.fromDate, this.i18n("format.date"))) {
			this.sendKeyError("validator.mask", "fromDate", "format.date");
		}
		
		Date fromDateObject = DateUtil.fromString(this.fromDate, this.i18n("format.date"));
		Date toDateObject = DateUtil.fromString(this.toDate, this.i18n("format.date"));
		if(!Checker.isEmpty(this.toDate)) {
			if(!Checker.isDate(this.toDate, this.i18n("format.date"))) {
				this.sendKeyError("validator.mask", "toDate", "format.date");
			}
			if(fromDateObject.after(toDateObject)) {
				this.sendKeyError("validator.greater", "fromDate", "toDate");
			}
		}
		
		Collection<Date> teachingDays = OfferingService.INSTANCE.findTeachingDays(this.offeringId, fromDateObject, toDateObject);
		if(teachingDays.isEmpty()) {
			this.sendKeyError("validator.invalid", "date");
		}
		return this.toSuccess();
	}

	public Long[] getMemberIds() {
		return memberIds;
	}

	public void setMemberIds(Long[] memberIds) {
		this.memberIds = memberIds;
	}

	public String getFromDate() {
		return fromDate;
	}

	public void setFromDate(String fromDate) {
		this.fromDate = fromDate;
	}

	public String getToDate() {
		return toDate;
	}

	public void setToDate(String toDate) {
		this.toDate = toDate;
	}

	public Long getOfferingId() {
		return offeringId;
	}

	public void setOfferingId(Long offeringId) {
		this.offeringId = offeringId;
	}
}
